package com.manage.DB;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.manage.handler.ProjectUserHandler;
import com.manage.handler.SqliteHelper;

public class ProjectUserController {
	// 数据库名称
	private static String DB_NAME = "bugmanage.db";
	// 数据库版本
	private static int DB_VERSION = 2;
	private SQLiteDatabase db;
	private SqliteProjectUserHelper dbSinaHelper;

	// private SqliteHelper dbHelper;

	public ProjectUserController(Context context) {
		dbSinaHelper = new SqliteProjectUserHelper(context, DB_NAME, null, DB_VERSION);
		// dbHelper = new SqliteHelper(context, DB_NAME, null, DB_VERSION);
		db = dbSinaHelper.getWritableDatabase();
	}

	public void Close() {
		db.close();
		dbSinaHelper.close();
	}



	// 创建表
	// 获取users表中的USER_ID、Access Token、Access Secret的记录
	public List<ProjectUserHandler> GetUserList(Boolean isSimple) {
		List<ProjectUserHandler> userList = new ArrayList<ProjectUserHandler>();
		Cursor cursor = db.query(SqliteProjectUserHelper.TB_NAME, null, null, null, null, null, ProjectUserHandler.PROJECT_ID + " DESC");
		cursor.moveToFirst();
		while (!cursor.isAfterLast() && (cursor.getString(1) != null)) {
			ProjectUserHandler user = new ProjectUserHandler();
			user.setProjectId(cursor.getString(0));
			user.setUserName(cursor.getString(1));
			user.setUseId(cursor.getString(2));

			userList.add(user);
			cursor.moveToNext();
		}
		cursor.close();
		return userList;
	}
	public boolean isNullSinaTb() {
		Boolean flag = false;
		Cursor cursor = db.query(SqliteProjectUserHelper.TB_NAME, null, null, null, null, null, ProjectUserHandler.PROJECT_ID + " DESC");
		flag = !cursor.moveToFirst();
		cursor.close();
		return flag;
	}

	// 判断users表中的是否包含某个USER_ID的记录
	public boolean HaveUserInfo(String userId) {
		Boolean b = false;
		Cursor cursor = db.query(SqliteProjectUserHelper.TB_NAME, null, ProjectUserHandler.PROJECT_ID + "=" + userId, null, null, null,
				null);
		b = cursor.moveToFirst();
		// Log.e("HaveUserInfo", b.toString());
		cursor.close();
		return b;
	}

	public final static String PROJECT_ID ="project_id";
	public final static String USER_NAME = "user_name";
	public final static String USER_ID ="user_id";
	// 更新users表的记录
	public int UpdateUserInfo(ProjectUserHandler user, Boolean isSimple) {
		ContentValues values = new ContentValues();
		// values.put(ProjectUserHandler.PROJECT_ID, user.getPorjectId());
		values.put(ProjectUserHandler.PROJECT_ID, user.getProjectId());
		values.put(ProjectUserHandler.USER_NAME, user.getUserName());
		values.put(ProjectUserHandler.USER_ID, user.getUseId());

		int id = db.update(SqliteProjectUserHelper.TB_NAME, values, ProjectUserHandler.PROJECT_ID + "=" + user.getProjectId(), null);
		// Log.e("UpdateUserInfo", id + "");
		return id;
	}

	// 添加users表的记录
	public Long SaveUserInfo(ProjectUserHandler user, Boolean isSimple) {
		ContentValues values = new ContentValues();
		values.put(ProjectUserHandler.PROJECT_ID, user.getProjectId());
		values.put(ProjectUserHandler.PROJECT_ID, user.getProjectId());
		values.put(ProjectUserHandler.USER_NAME, user.getUserName());
		values.put(ProjectUserHandler.USER_ID, user.getUseId());
		Long uid = db.insert(SqliteHelper.TB_NAME, ProjectUserHandler.PROJECT_ID, values);
		// Log.e("SaveUserInfo", uid + "");
		return uid;
	}

	// 删除users表的记录
	public int DelUserInfo(String userId) {
		int id = db.delete(SqliteHelper.TB_NAME, ProjectUserHandler.PROJECT_ID + "=" + userId, null);
		// Log.e("DelUserInfo", id + "");
		return id;
	}
}
